package news.paper.tintuc.tinnong.docbao.doctin;

import news.paper.tintuc.tinnong.docbao.doctin.GCM.PopupAD;
import news.paper.tintuc.tinnong.docbao.doctin.fragment.Fragment_ListNews;
import news.paper.tintuc.tinnong.docbao.doctin.fragment.MenuFragment;
import news.paper.tintuc.tinnong.docbao.doctin.util.Constrant;
import tienich360.com.lib.LinkStore;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.actionbarsherlock.view.MenuItem;
import com.google.analytics.tracking.android.EasyTracker;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.startapp.android.publish.StartAppAd;
import com.startapp.android.publish.StartAppSDK;
import com.startapp.android.publish.banner.Banner;

public class Activity_Menu extends BaseActivity_Menu {

	private Fragment mContent;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		StartAppSDK.init(this, "105853845", "210033017", true);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		if (getSupportActionBar() != null)
			getSupportActionBar().hide();
		setBehindContentView(R.layout.test_smmenu_frame);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.menu_frame, new MenuFragment()).commit();
		SlidingMenu sm = getSlidingMenu();
		getSlidingMenu().setMode(SlidingMenu.LEFT);
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		// sm.setBehindWidthRes(R.dimen.slidingmenu_width_cat);
		sm.setBehindWidth(Constrant.WIDTH * 4 / 5);
		if (savedInstanceState != null)
			mContent = getSupportFragmentManager().getFragment(
					savedInstanceState, "mContent");
		if (mContent == null)
			mContent = new Fragment_ListNews(Activity_Menu.this);

		setContentView(R.layout.test_smcontent_frame);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_frame, mContent).commit();
		PopupAD popupAD = new PopupAD();
		popupAD.show_popupad(20000, Activity_Menu.this);
		try {
			Constrant.selectedPosition = 1;
			if (Constrant.adapter != null)
				Constrant.adapter.notifyDataSetChanged();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			toggle();
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		getSupportFragmentManager().putFragment(outState, "mContent", mContent);
	}

	public void switchContent(int position) {
		((Fragment_ListNews) mContent).re_int(position);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_frame, mContent).commit();
		getSlidingMenu().showContent();
		Constrant.adapter.notifyDataSetChanged();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (getSlidingMenu().isMenuShowing()) {
				getSlidingMenu().toggle();
			} else {
				LinkStore linkStore = new LinkStore();
				linkStore.showExitDialog_tienich360(Activity_Menu.this);
			}
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_MENU) {
			getSlidingMenu().toggle();
			return true;
		} else
			return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onStart() {
		super.onStart();
		EasyTracker.getInstance(this).activityStart(this); // Add this method.
	}

	@Override
	public void onStop() {
		super.onStop();
		EasyTracker.getInstance(this).activityStop(this); // Add this method.
	}

}